package com.crm.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.crm.model.mdao.ProductMapper;
import com.crm.model.pojo.Orderinfo;
import com.crm.model.pojo.Orderitem;
import com.crm.model.pojo.Product;
import com.crm.model.pojo.Returnmoney;
import com.crm.services.CustomerServices;
import com.crm.services.OrderInfoServers;
import com.crm.services.OrderitemsServices;
import com.crm.services.ProductServices;
import com.crm.services.UserServices;
import com.github.pagehelper.PageInfo;

@Controller
public class OrderitemsController {
	
	@Autowired
	OrderitemsServices orderitemsService;

	@Autowired
	UserServices userServer;
	@Autowired
	OrderInfoServers orderServer;
	@Autowired
	ProductServices productServices;
	@Autowired
	CustomerServices cumService;
	@Autowired
	ProductMapper productMapper;
	/**
	 * 分页跳转到交付计划/订单订情页
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@GetMapping("/sales/prosale/orderitem")
	public ModelAndView order(Integer pageNum, Integer pageSize,Integer statusid ,String search) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		ModelAndView mv = new ModelAndView("sales/prosale/orderitem");
		//分页查询订单详情
		List<Map<String, String>> findAllMaper = orderitemsService.selectByPager(pageNum, pageSize,statusid,search);
		System.out.println(findAllMaper);
		String jsonString = JSONObject.toJSONString(findAllMaper);
		//获取分页信息
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(findAllMaper);
		System.out.println(pager);
		String json = JSONObject.toJSONString(pager);
		System.out.println(jsonString);
		mv.addObject("list", jsonString);
		mv.addObject("page", pager);
		mv.addObject("pagejson", json);		
		return mv;
	}
	/**
	 * 分页查询交付计划/订单订情页
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@PostMapping("/sales/prosale/orderitem")
	@ResponseBody	
	public PageInfo<Map<String, String>> orderitem(Integer pageNum, Integer pageSize,Integer statusid ,String search) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 5;
		List<Map<String, String>> selectAll = orderitemsService.selectByPager(pageNum, pageSize,statusid,search);
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(selectAll);
		return pager;
	}
	
	/**
	 * 重定向到编辑订单详情页
	 * @return 
	 * @return 
	 * @return
	 */
	@GetMapping("/sales/prosale/orderItemView")
	public ModelAndView orderView(Integer id,Integer pageNum, Integer pageSize,String search) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 9;
		ModelAndView mv = new ModelAndView("/sales/prosale/orderItemView");
		//产品列表
		List<Map<String, String>> productAllPage = orderitemsService.selectByPager1(pageNum, pageSize,search);
		//获取分页信息
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(productAllPage);
		System.out.println(pager);
		String json = JSONObject.toJSONString(pager);
		//订单详情相关数据
		List<Map<String, String>> selectOrderItemView = orderitemsService.selectOrderItemView(id);
		String jsonw = JSONObject.toJSONString(productAllPage);
		String json1 = JSONObject.toJSONString(selectOrderItemView);
		System.out.println(json1);
		System.out.println(json);
		mv.addObject("oid", id);
		mv.addObject("orderItemView", json1);
		mv.addObject("proAll", jsonw);
		mv.addObject("page", pager);
		mv.addObject("pagejson", json);		
		System.out.println("编辑订单视图页");
		return mv;
	}
	
	/**
	 * 分页查询产品
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@PostMapping("/sales/prosale/orderItemView")
	@ResponseBody	
	public PageInfo<Map<String, String>> orderPage(Integer pageNum, Integer pageSize,Integer statusid ,String search) {
		if (pageNum == null)
			pageNum = 1;
		if (pageSize == null)
			pageSize = 9;
		List<Map<String, String>> selectAll = orderitemsService.selectByPager1(pageNum, pageSize,search);
		PageInfo<Map<String, String>> pager = new PageInfo<Map<String, String>>(selectAll);
		return pager;
	}
	
	/**
	 * 用于查询加载回款记录界面数据
	 * 
	 * @return
	 */
	@PostMapping("/sales/prosale/addoipage")
	@ResponseBody
	public JSONObject addOrderpage() {
		// 用于加载客户信息
		List<Map<String, String>> selectAllCustomer = cumService.selectAllCustomer();
		// 用于加载所有者信息
		List<Map<String, String>> selectUserAll = userServer.selectUserAll();
		//加载界面产品数据
		List<Map<String, String>> productAllPage = orderitemsService.productAllPage();
		System.out.println(selectAllCustomer);
		JSONObject jsonarray = new JSONObject();
		jsonarray.put("customer", selectAllCustomer);
		jsonarray.put("userAll", selectUserAll);
		jsonarray.put("product", productAllPage);
		return jsonarray;
	}
	
	/**
	 * 增加订单明细
	 * @param comcustid
	 * @param orderid
	 * @param date1
	 * @param bmpmoney
	 * @param radiosatus
	 * @param uid
	 * @param textcommunication
	 * @return
	 */
	@PostMapping("/sales/prosale/addOrderItem")
	@ResponseBody
	public String addOrder(@RequestParam(name = "pname", required = false) Integer pname,
			@RequestParam(name = "comcustid", required = false) Integer comcustid,
			@RequestParam(name = "orderid", required = false) Integer orderid,
			@RequestParam(name = "price", required = false) String price,
			@RequestParam(name = "money", required = false) String money,
			@RequestParam(name = "discount", required = false) String discount,
			@RequestParam(name = "count", required = false) String count,
			@RequestParam(name = "date", required = false) String date,
			@RequestParam(name = "uid", required = false) Integer uid,
			@RequestParam(name = "require", required = false) String require,
			@RequestParam(name = "conditions", required = false) String conditions,
			@RequestParam(name = "remark", required = false) String remark) {
		System.out.println(orderid);
		// 根据ID查询客户
		Orderinfo orderinfo = orderServer.findById(orderid);
		System.out.println("订单："+orderinfo);
		System.out.println(pname);
		Product product = productServices.findOrderItemToPrd(pname);
		System.out.println(product);
		Orderitem orderitem =new Orderitem();
		orderitem.setProduct(product);
		orderitem.setOrderinfo(orderinfo);
		orderitem.setCustId(comcustid);
		orderitem.setOiPrice(new BigDecimal(price));
		orderitem.setOiMoney(new BigDecimal(money));
		orderitem.setOiDiscount(new BigDecimal(discount));
		orderitem.setOiCount(new BigDecimal(count));
		orderitem.setOiDate(java.sql.Date.valueOf(date));
		orderitem.setConditions(conditions);
		orderitem.setRemark(remark);
		orderitem.setuId(uid);
		orderitem.setStatus(1102);
		Orderitem addItem = orderitemsService.addItem(orderitem,orderinfo);
		System.out.println(addItem);
		return "新增交付计划/订单明细";
	}
	

	/**
	 * 用于查询加载回款记录界面数据订单
	 * 
	 * @return
	 */
	@PostMapping("/sales/prosale/oiorderpage")
	@ResponseBody
	public JSONObject oiorderpage(@RequestParam("ID") Integer cumid) {
		// 用于加载所有者信息
		List<Map<String, String>> selectAllBypageID = orderServer.selectAllBypageID(cumid);
		JSONObject jsonarray = new JSONObject();
		System.out.println(selectAllBypageID);
		jsonarray.put("order", selectAllBypageID);
		return jsonarray;
	}
	
	/**
	 * 用于前台选择产品显示价格
	 * 
	 * @return
	 */
	@PostMapping("/sales/prosale/selectoprice")
	@ResponseBody
	public JSONObject bmporderpage(@RequestParam("ID") Integer proid) {
		// 用于加载所有者信息
		List<Map<String, String>> productoiPage = orderitemsService.productoiPage(proid);
		JSONObject jsonarray = new JSONObject();
		jsonarray.put("proprice", productoiPage);
		return jsonarray;
	}
	
	/**
	 * 用于编辑订单详情页保存订单详情
	 * @return 
	 * 
	 * @return
	 */
	@PostMapping("/sales/prosale/addorderitemview")
	@ResponseBody
	public String addorderitemview(@RequestParam("oitem") Object proid,@RequestParam("ID") Integer id) {
		String str= String.valueOf(proid);
		System.out.println(id);
		@SuppressWarnings("unchecked")
		List<Map<String,String>> limap = JSONArray.parseObject(str,List.class);
		orderitemsService.addorderitemview(limap,id);
		return "123";
	}
}
